package org.arabidopsis.plantCyc;

import org.biojavax.bio.seq.io.FastaFormat;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.biojava.bio.BioException;
import org.biojava.bio.seq.Sequence;
import org.biojava.bio.seq.SequenceIterator;
import org.biojava.bio.seq.db.SequenceDB;
import org.biojava.bio.seq.io.SeqIOTools;
import org.biojava.bio.seq.io.SymbolTokenization;
import org.biojava.bio.symbol.Alphabet;
import org.biojava.bio.symbol.AlphabetManager;
import org.biojavax.bio.seq.RichSequence;
import org.biojavax.bio.seq.RichSequenceIterator;
import org.biojavax.bio.seq.io.FastaFormat;
import org.biojavax.bio.seq.io.RichStreamReader;
import org.biojavax.bio.seq.io.FastaFormat;

public class EnzymeDist {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String fileName="/home/ozgur/brenda/5___BRENDA_sequences.txt";
		File myFile= new File(fileName);	
/*		FastaFormat fastaParser=new FastaFormat();
		try {
			System.out.println(fastaParser.guessSymbolTokenization(myFile).CHARACTER);
		} catch (IOException e) {
			e.printStackTrace();
		}
*/		
		InputStream is = new FileInputStream(fileName);
		RichStreamReader seqFileIter=new RichStreamReader(is,new FastaFormat(),SymbolTokenization.CHARACTER,null);
		
		//EnzymeDist.getEnzymes(fileName,seqIt);
		HashMap<ECnumberPair, ECdistances> enzymeMap=new HashMap();
		//int count=0;
		for (; seqIt.hasNext();) {
			try {
				Sequence currentSeq = (Sequence) seqIt.nextSequence();
				Matcher matcher =ECnumber.ecNumberPattern.matcher(currentSeq.getAnnotation().toString());
				if (matcher.find())
				{
					ECnumber ecNumber1=new ECnumber(matcher.group());
					//count++;
				}
				else System.out.println("not found ecNumber");			
			}		
			catch (BioException e) {
				e.printStackTrace();
			}
			
		}
		//System.out.println("found ecNumber for: "+count);
		//System.out.println("database size"+db[0].ids().size());
		}

	/**
	 * @param String[]
	 */
	private static boolean getEnzymes(String fileName, RichSequenceIterator seqIt) {
		try {
		      //setup file input
		      InputStreamReader is =
		          new InputStreamReader(new FileInputStream(fileName));
		 
		 
		      //get the appropriate Alphabet
		      //Alphabet alpha = AlphabetManager.alphabetForName(fileAndType[1]);
		 
		      //get a SequenceDB of all sequences in the file
		      //db[0] = SeqIOTools.readFasta(is, alpha);
		      seqIt = RichSequence.IOTools.readFastaProtein(new BufferedReader(is), null);
		      SequenceIterator seqIt=db[0].sequenceIterator();
		      int count=0;
		      while(seqIt.hasNext() && count<10)
		      {
		    	  Sequence currentSeq=seqIt.nextSequence();
		    	  count++;
		    	  System.out.println("next name in fasta file is " + currentSeq.getName());
		    	  System.out.println(currentSeq.getURN());
		    	  System.out.println(currentSeq.getAnnotation());
 		      	  System.out.println("Length: " + currentSeq.length());
 				  System.out.println("Features: " + currentSeq.countFeatures());

		    	  System.out.println("sequence string: "+ currentSeq.seqString());
		      }
		      System.out.println("Number of entries: "+count);
		      System.out.print("Done reading Fasta File");
		      return true;
	  	  	}
		    catch (BioException ex) {
		      //not in fasta format or wrong alphabet
		      ex.printStackTrace();
		      System.out.println("ERROR 1");
		      return false; 
		    }
		    catch (NoSuchElementException ex) {
		      //no fasta sequences in the file
		      ex.printStackTrace();
		      System.out.println("ERROR 2");
		      return false; 
		    }
		    catch (FileNotFoundException ex) {
		      //problem reading file
		      ex.printStackTrace();
		      System.out.println("ERROR 3");
		      return false; 
		    }
		}
}